define(["jquery","bbcare","fu/app/comm/comm",
	"scprit/nutrition/app/courseLibrary/courseLibrary.ui.js",
    "scprit/nutrition/app/courseLibrary/courseLibrary.servers.js"
    ],function($,$bb,$comm,$ui,$servers){
	var event = {};

	/**
	 * 新增疗程库成功后的回调函数
	 */
	var ajaxCourseLibirayModifyCallBack = function(data,libraryId,className){
		var str = libraryId ? "修改疗程库名称成功！" : "添加疗程库成功！";
		$("." + className).modal("hide");
		$bb.toastShow(str);
		courseLibirayList();
	}

	/**
	 * 点击form表单里的确定按钮，进行添加/修改
	 */
	var onSaveCourseLibiray = function(){
		var libraryName = $(this).parents("form").find("input[name='libraryName']").val();
		var remark = $(this).parents("form").find("input[name='remark']").val();
		var libraryId = $(this).attr("libraryId");
		var className = $(this).data("className");
		var params = {
		    "libraryName":libraryName,
		    "remark":remark,
		    "libraryId":libraryId
		}
		$servers.ajaxCourseLibirayModify(params,ajaxCourseLibirayModifyCallBack,className);
	}

	/**
	 * 点击新建疗程库，将form表单渲染出来
	 */
	var onAddCourseLibiray = function(){
		var className = "addCourseLibirayModal";
		$comm.modal({
			"className":className,
			"tit":"添加疗程库"
		},function(){
			$ui.addCourseLibirayFormInit({
				"dom":$("." + className + " .modal-body"),
			});
			$("." + className).modal("show");
			$("#saveCourseLibirayBtn").unbind("click");
			$("#saveCourseLibirayBtn").data({"className":className});
			$("#saveCourseLibirayBtn").click(onSaveCourseLibiray);
		});
	}

	/**
	 * 修改疗程库名称
	 */
	var onSetCourseLibrary = function(){
		var data = $(this).data("data");
		var libraryId = $(this).parents(".course-box").attr("libraryId");
		var originalData = $servers.getSetCourseLibraryOriginalData(data,libraryId);
		var className = "setCourseLibirayModal";
		$comm.modal({
			"className":className,
			"tit":"修改疗程库"
		},function(){
			$ui.addCourseLibirayFormInit({
				"dom":$("." + className + " .modal-body"),
				"data":originalData
			});
			$("." + className).modal("show");
			$("#saveCourseLibirayBtn").unbind("click");
			$("#saveCourseLibirayBtn").data({"className":className});
			$("#saveCourseLibirayBtn").click(onSaveCourseLibiray);
		});
	}

	/**
	 * 跳转到编辑页面
	 */
	var onEditCourseLibrary = function(){
		var libraryId = $(this).parent(".course-box").attr("libraryId");
		$bb.openPage("editCourseLibrary",libraryId);
	}

	/**
     * 查询疗程库成功后的回调函数
     */
    var ajaxCourseLibirayListCallBack = function(data){
        $ui.courseLibirayListInit({
            "id":"courseLibirayList",
            "list":data
        });
        $("#courseLibirayList .course-box").mouseenter(function(){
        	$(this).find(".course-name i").removeClass("fa-folder-o").addClass("fa-folder-open-o");
        	$(this).find(".course-operation").slideDown();
        }).mouseleave(function(){
        	$(this).find(".course-name i").removeClass("fa-folder-open-o").addClass("fa-folder-o");
        	$(this).find(".course-operation").slideUp();
        });
        // 跳转到疗程库详情页
        $("#courseLibirayList .course-box .course-info").unbind("click");
        $("#courseLibirayList .course-box .course-info").click(onEditCourseLibrary);
        // 添加疗程库
        $("#addCourseLibiray").unbind("click");
        $("#addCourseLibiray").click(onAddCourseLibiray);
        // 修改疗程库名称
        $("#courseLibirayList .course-box .setCourseLibrary").unbind("click");
        $("#courseLibirayList .course-box .setCourseLibrary").data({"data":data});
        $("#courseLibirayList .course-box .setCourseLibrary").click(onSetCourseLibrary);
    }

    /**
     * 查询疗程库
     */
    var courseLibirayList = function(){
    	$servers.ajaxCourseLibirayList(ajaxCourseLibirayListCallBack);
    }
    event.courseLibirayList = function(){
    	courseLibirayList();
    }

	return event;
});